package com.um.springboot.starter.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.um.springboot.starter.properties.HshProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Title 支付工具类
 * @Description
 * @Author renjunwei
 * @Date 2022/1/20 2:03 PM
 */
@Component
public class PayUtil {

    @Value("${spring.profiles.active}")
    private String profile;

    public static String staticProfile;

    @Autowired
    private HshProperties hshProperties;

    public static HshProperties staticHshProperties;

    @PostConstruct
    public void init() {
        staticProfile = profile;
        staticHshProperties = hshProperties;
    }


    /**
     * 转为total_fee
     * @param amount
     * @return
     */
    public static int toTotalFee(int amount){
        if (!isProd()) {
            return staticHshProperties.getConfig().getDevAmount();
        }

        return amount;
    }

    /**
     * 转为total_fee
     * @param amount
     * @return
     */
    public static String toTotalFeeStr(int amount){
        return String.valueOf(toTotalFee(amount));
    }

    public static double toDoubleAmount(int amount){
        double result = NumberUtil.div(amount, 100);
        if (!isProd()) {
            return NumberUtil.div(staticHshProperties.getConfig().getDevAmount().intValue(), 100);
        }
        return result;
    }

    public static String toDoubleAmountStr(int amount){
        return String.valueOf(toDoubleAmount(amount));
    }

    /**
     * 判断是否是生产环境
     * @return
     */
    public static boolean isProd(){
        return staticProfile.indexOf("prod") >= 0;
    }

    /**
     * 判断是否是开发环境
     * @return
     */
    public static boolean isDev(){
        return staticProfile.indexOf("dev") >= 0;
    }

    /**
     * 获取交易号
     * @return
     */
    public static String getOutTradeNo(){
        return StrUtil.builder(DateUtil.date().toString(DatePattern.PURE_DATETIME_MS_PATTERN), RandomUtil.randomNumbers(5)).toString();
    }


}
